<?php
	class Disposisi_surat_model extends Model{
		function Disposisi_surat_model(){
			parent::Model();
		}

		function _generateNomorSurat() {
			$sql = 'SELECT MAX(`pta_arsip_surat`.`nomor_arsip`) AS nomor_arsip '.
			       'FROM (`pta_arsip_surat`) JOIN `pta_arsip` ON `pta_arsip`.`idpta_arsip`=`pta_arsip_surat`.`pta_id_arsip` '.
			       'WHERE `pta_arsip`.`pta_id_kategori_arsip` = '.DISPOSISI;
			$q = $this->db->query($sql);
			if ($q->num_rows() > 0) {
				$row = $q->row();
				return ($row->nomor_arsip + 1);
			} else {
				return 1;
			}
		}
		
		function list_header($ro=false) {
			if ($this->session->userdata('role')==GROUP_ADMIN) {
	            $s = "<table id=\"scrollable\" width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"1\" class=\"tbl\">".
	                 "<thead><tr><th>No</th><th>Isi Ringkasan</th><th>No. Surat</th><th>No. Arsip</th><th>Lokasi Arsip</th><th>Tgl Surat</th><th>Tgl Diterima</th>".
	                 "<th>Asal Surat</th><th>Terusan</th>";
			} else {
	            $s = "<table id=\"scrollable\" width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"1\" class=\"tbl\">".
	                 "<thead><tr><th>No</th><th>Perihal</th><th>Kode</th><th>No. Surat</th><th>Lokasi Arsip</th><th>Tgl Surat</th><th>Tgl Diterima</th>".
	                 "<th>Asal Surat</th><th>Terusan</th>";
			}
            if ($ro==false) {
                $s .= "<th colspan=4> Aksi </th>";
            }
            $s .= "</tr></thead><tbody>";
            return $s;
		}
		
        function list_isi($where="", $order="", $ro=false) {
        	$this->db->where(array('pta_id_kategori_arsip' => DISPOSISI));
            if ($where!='') {
                $this->db->where($where);
            }
            if ($order == "") {
                $this->db->order_by('tgl_dokumen desc');	//tgl_dokumen = tgl_diterima
            } else {
                $this->db->order_by($order);
            }
            $q = $this->db->get('arsip');
            
            $s = "[";
			$i = 1;
            $order   = array("\r\n", "\n", "\r", "'");
            $replace = ' ';
            $this->load->model('Datum_model');
            foreach($q->result() as $row) {
                if ($i % 2 == 1) {
                    $kelas = "event2";
                } else {
                    $kelas = "event";
                }
                
                $this->db->where('pta_id_arsip ='.$row->idpta_arsip.'');       
                $q0 = $this->db->get('arsip_surat');
                if ($q0->num_rows() > 0) {
                	$row0 = $q0->row();
                	
                	$kode_surat          = $row0->kode_surat;
                	$nomor_arsip         = $row0->nomor_arsip;
                	$lokasi_arsip        = $row0->lokasi_arsip;
                	$tgl_arsip           = YMD2DMY(trimTime($row0->tgl_arsip), TGL_SEPARATOR);
                	$tgl_diterima        = YMD2DMY(trimTime($row0->tgl_diterima), TGL_SEPARATOR);
                	$asal                = sprintf('%s ', $row0->nama_pembuat);
					
                		$tujuan          = sprintf('%s ', $row0->nama_penerima, $row0->jabatan_penerima);
                		$terusan         = sprintf('%s ', $row0->nama_terusan);
					
                
                
                $s .= "['<tr class=\"$kelas\" id=\"row-".$row->idpta_arsip."\" ><td><div align=\"right\">$i.&nbsp;&nbsp;&nbsp;</div></td>".
                      //"<td>".str_replace($order, $replace, $row->judul)."</td>".
                      "<td>".str_replace($order, $replace, $row->deskripsi)."</td>".
                      "<td><div align=\"center\">".$kode_surat."</div></td>".
                      "<td><div align=\"center\">".$nomor_arsip."</div></td>".
                      "<td><div align=\"center\">".$lokasi_arsip."</div></td>".
                      "<td><div align=\"center\">".$tgl_arsip."</div></td>".
                      "<td><div align=\"center\">".$tgl_diterima."</div></td>".
                      "<td><div align=\"center\">".$asal."</div></td>";      
                
                	//$s .= "<td><div align=\"center\">".$tujuan."</div></td>".
                	  $s .="<td><div align=\"center\">".$terusan."</div></td>"; 
                    
                
                    if($row0->nama_terusan==$this->session->userdata('username'))
                      $s .= "<td><div align=\"center\"><a href=\"".base_url()."index_arsip.php/surat_disposisi/buat_disposisi/".$row->idpta_arsip."\" title=\"Disposisi\" >Disposisi</a></div></td>";
                     
                       
                if ($this->session->userdata('role')==GROUP_ADMIN) {
    				$id_file = Datum_model::getData('pta_id_files', 'arsip', array('idpta_arsip'=>$row->idpta_arsip));
    				$namafile = Datum_model::getData('namafile', 'files', array('idpta_files'=>$id_file));
                	$tipe_file = Datum_model::getData('tipe_file', 'files', array('idpta_files'=>$id_file));
                	if (strpos($tipe_file, 'image') === false) {
                		$ling = "<a href=\"".base_url()."uploads/arsip/".$row->idpta_arsip.$namafile."\" title=\"Preview Dokumen\" target=\"_blank\" class=\"edit\"><img src=\"".base_url()."templates/images/printview.gif\" border=\"0\"></a>";
                	} else {
                		$ling = "<a href=\"".base_url()."index_arsip.php/disposisi/preview/".$row->idpta_arsip."\" title=\"Preview Dokumen\" target=\"_blank\" class=\"edit\"><img src=\"".base_url()."templates/images/printview.gif\" border=\"0\"></a>";
                	}
                	
                	$s .= "<td><div align=\"center\"><a href=\"".base_url()."index_arsip.php/surat_disposisi/buat_disposisi/".$row->idpta_arsip."\" title=\"Disposisi\" >Disposisi</a></div></td>"; 
                    $s .= "<td><div align=\"center\">$ling</div></td>". 
                          "<td><div align=\"center\"> <a href=\"".base_url()."index_arsip.php/surat_disposisi/form_edit/".$row->idpta_arsip."\" title=\"Ubah Data\" class=\"edit\"><img src=\"".base_url()."templates/images/edit.gif\" border=\"0\"></a></div></td>".   
                          "<td><div align=\"center\"><a href=\"javascript:deleteRow(".$row->idpta_arsip.")\"><img src=\"".base_url()."templates/images/del.gif\" border=\"0\"></a></div></td>";
                }  
                
                } 
                $s .= "</tr>'],";

                $i++;
            }
            $s = substr($s, 0, -1);
            $s .= '];';
            return $s;
        }

        function entry_form($form_id, $form_action, $arr=array()) {
        	$ret = "
			<form id=\"".$form_id."\" action=\"".$form_action."\" method=\"post\" enctype=\"multipart/form-data\">
			<fieldset >
			<table class=\"formTabel\" border=\"0\" align=\"center\" cellpadding=\"5\" cellspacing=\"1\">
			  <tr>
                <td class=\"kolom\" valign=\"top\">Indeks Berkas</td>
                <td valign=\"top\" width=\"5\">:</td>
                <td valign=\"top\"><input type=\"text\" name=\"nomor_arsip\" id=\"nomor_arsip\" size=\"8\" value=\"".(isset($arr['nomor_arsip'])?$arr['nomor_arsip']:$this->_generateNomorSurat())."\" />
                &nbsp;&nbsp;Kode :&nbsp;&nbsp;<input type=\"text\" name=\"lokasi_arsip\" id=\"lokasi_arsip\" size=\"8\" value=\"".(isset($arr['lokasi_arsip'])?$arr['lokasi_arsip']:"")."\" /></td>
              </tr>
              <tr>
                <td class=\"kolom\" valign=\"top\">Tanggal</td>
                <td valign=\"top\">:</td>
                <td valign=\"top\"><input type=\"text\" class=\"date-pick dp-applied\" id=\"tgl_dokumen\" name=\"tgl_dokumen\" size=\"38\" value=\"".(isset($arr['tgl_dokumen'])?$arr['tgl_dokumen']:date('d-m-Y'))."\" /></td>
              </tr>
              <tr>
                <td class=\"kolom\" valign=\"top\">Nomor Surat</td>
                <td valign=\"top\" width=\"5\">:</td>
                <td valign=\"top\"><input type=\"text\" name=\"kode_surat\" id=\"kode_surat\" size=\"38\" value=\"".(isset($arr['kode_surat'])?$arr['kode_surat']:"")."\" /></td>
              </tr>
              <tr>
                <td class=\"kolom\" valign=\"top\">Asal</td>
                <td valign=\"top\" width=\"5\">:</td>
                <td valign=\"top\"><input type=\"text\" name=\"nama_pembuat\" id=\"nama_pembuat\" size=\"38\" value=\"".(isset($arr['nama_pembuat'])?$arr['nama_pembuat']:"")."\" /></td>
              </tr>
              <tr>
                <td class=\"kolom\" valign=\"top\">Diterima Tanggal </td>
                <td valign=\"top\">:</td>
                <td valign=\"top\"><input type=\"text\" class=\"date-pick dp-applied\" id=\"tgl_diterima\" name=\"tgl_diterima\" size=\"38\" value=\"".(isset($arr['tgl_diterima'])?$arr['tgl_diterima']:date('d-m-Y'))."\" /></td>
              </tr>
     
			         
			  <tr>
                <td valign=\"top\">Isi Ringkasan</td>
                <td valign=\"top\">:</td>
                <td valign=\"top\"><textarea name=\"deskripsi\" id=\"deskripsi\" cols=\"45\" rows=\"5\">".(isset($arr['deskripsi'])?$arr['deskripsi']:"")."</textarea></td>
              </tr>   

			  <tr>
			    <td class=\"kolom\" valign=\"top\">File</td>
			    <td valign=\"top\">:</td>
			    <td valign=\"top\"><input type=\"file\" name=\"namafile\" id=\"namafile\" size=\"20\" /> <i>".(isset($arr['namafile'])?$arr['namafile']:"")."</i></td>
			  </tr>
			  
			  
			  ";
        	if (isset($arr['created_by'])) {
        		$ret .= "
			  <tr>
			    <td class=\"kolom\" valign=\"top\">Dibuat oleh</td>
			    <td valign=\"top\">:</td>
			    <td valign=\"top\"><i>".(isset($arr['created_by'])?$arr['created_by']:"")."</i></td>
			  </tr>";
        	}
        	if (isset($arr['last_update'])) {
        		$ret .= "
			  <tr>
			    <td class=\"kolom\" valign=\"top\">Terakhir di update oleh</td>
			    <td valign=\"top\">:</td>
			    <td valign=\"top\"><i>".(isset($arr['last_update'])?$arr['last_update']:"")."</i></td>
			  </tr>";
        	}
        	$ret .= "
			  <tr>
			    <td valign=\"top\">&nbsp;</td>
			    <td valign=\"top\">&nbsp;</td>
			    <td valign=\"top\">
			      <input class=\"tombol\" type=\"button\" name=\"btn_kembali\" id=\"btn_kembali\" value=\"<< Kembali\" />
			      <input class=\"tombol\" type=\"submit\" name=\"button\" id=\"button\" value=\"   Simpan   \" />
			    </td>
			  </tr>
			</table>
			</fieldset>
			</form>
			  ";
        	
        	return $ret;
        }

        function entry_disposisi($form_id, $form_action, $arr=array()) {
            $id_arsip_surat = (isset($arr['id_arsip_surat'])?$arr['id_arsip_surat']:"");
            
            if($id_arsip_surat!="")
            $this->db->where(array('id_arsip_surat' => $id_arsip_surat));
            $this->db->order_by('idpta_arsip_terusan');
            
            $q1 = $this->db->get('arsip_terusan');
            
                        
            $ret = "
            <form id=\"".$form_id."\" action=\"".$form_action."\" method=\"post\" enctype=\"multipart/form-data\">
            <fieldset >
            <table class=\"formTabel\" border=\"0\" align=\"center\" cellpadding=\"5\" cellspacing=\"1\">
              <tr>
                <td class=\"kolom\" valign=\"top\">Indeks Berkas</td>
                <td valign=\"top\" width=\"5\">:</td>
                <td valign=\"top\">".(isset($arr['nomor_arsip']))."
                &nbsp;&nbsp;Kode :&nbsp;&nbsp;".(isset($arr['lokasi_arsip'])?$arr['lokasi_arsip']:"")."</td>
              </tr>
              <tr>
                <td class=\"kolom\" valign=\"top\">Tanggal</td>
                <td valign=\"top\">:</td>
                <td valign=\"top\">".(isset($arr['tgl_dokumen'])?$arr['tgl_dokumen']:date('d-m-Y'))."</td>
              </tr>
              <tr>
                <td class=\"kolom\" valign=\"top\">Nomor</td>
                <td valign=\"top\" width=\"5\">:</td>
                <td valign=\"top\">".(isset($arr['kode_surat'])?$arr['kode_surat']:"")."</td>
              </tr>
              <tr>
                <td class=\"kolom\" valign=\"top\">Asal</td>
                <td valign=\"top\" width=\"5\">:</td>
                <td valign=\"top\">".(isset($arr['nama_pembuat'])?$arr['nama_pembuat']:"")."</td>
              </tr>
              <tr>
                <td class=\"kolom\" valign=\"top\">Diterima Tanggal </td>
                <td valign=\"top\">:</td>
                <td valign=\"top\">".(isset($arr['tgl_diterima'])?$arr['tgl_diterima']:date('d-m-Y'))."</td>
              </tr>
     
                     
              <tr>
                <td valign=\"top\">Isi Ringkasan</td>
                <td valign=\"top\">:</td>
                <td valign=\"top\"><div>".(isset($arr['deskripsi'])?$arr['deskripsi']:"")."</div></td>
              </tr>   

              <tr>
                <td class=\"kolom\" valign=\"top\">File</td>
                <td valign=\"top\">:</td>
                <td valign=\"top\"><a href=\"".base_url().BASE_UPLOAD_ARSIP.(isset($arr['id'])?$arr['id']:"").(isset($arr['namafile'])?$arr['namafile']:"")."\" title=\"Preview Arsip\" target=\"_blank\" class=\"edit\"><i>".(isset($arr['namafile'])?$arr['namafile']:"")."</i></td>
              </tr>
              <tr>
                <td valign=\"top\">Diteruskan kepada</td>
                <td valign=\"top\">:</td>
                <td valign=\"top\">";
                 $x=1; 
                 foreach($q1->result() as $row1){
                 $id = $row1->idpta_arsip_terusan;    
                 $edit = "<div style=\"float:left; \">&nbsp;&nbsp;<a href=\"javascript:edit_terusan(".$id.")\">(edit) </a></div>";
                     
                 $ret .= "<div style=\"float:left; clear:both\"> " .$x. '.&nbsp; </div> <div style="float:left" id="textBox-'.$id.'">'.$row1->diteruskan_kepada."</div> ".$edit."<br>";                  
                 $x++;
                 }
                 
            
               $ret .="<input type=\"text\" name=\"nama_terusan\" id=\"nama_terusan\" size=\"38\" value=\"".(isset($arr['nama_terusan'])?$arr['nama_terusan']:"")."\" /></td>
              </tr>
              <tr>
                <td valign=\"top\">Isi Disposisi</td>
                <td valign=\"top\">:</td>
                <td valign=\"top\">";
                $y=1; 
                foreach($q1->result() as $row1){
                $id = $row1->idpta_arsip_terusan;
                $edit = "<div style=\"float:left; \">&nbsp;&nbsp;<a href=\"javascript:edit_disposisi(".$id.")\">(edit) </a></div>";  
                 if($row1->isi_disposisi==""){
                    $dsp = "-";                 
                 } else {
                    $dsp = "<div style=\"float:left\" id=\"textArea-$id\">".$row1->isi_disposisi."</div> ".$edit                         ."<div style=\"font-size:9px; float:left; clear:both\"> dari : ".$row1->username." (".$row1->tgl_dibuat.")</div>";            
                 }
                      
                 $ret .="<div style=\"float:left; clear:both\">$y. &nbsp; </div> $dsp <br><br>";                  
                 $y++;
                 
                 
                 }
                 
                $ret .="<textarea name=\"isi_disposisi\" id=\"isi_disposisi\" cols=\"45\" rows=\"5\">".(isset($arr['isi_disposisi'])?$arr['isi_disposisi']:"")."</textarea></td>
              </tr>
              
              ";
/*            if (isset($arr['created_by'])) {
                $ret .= "
              <tr>
                <td class=\"kolom\" valign=\"top\">Dibuat oleh</td>
                <td valign=\"top\">:</td>
                <td valign=\"top\"><i>".(isset($arr['created_by'])?$arr['created_by']:"")."</i></td>
              </tr>";
            }
            if (isset($arr['last_update'])) {
                $ret .= "
              <tr>
                <td class=\"kolom\" valign=\"top\">Terakhir di update oleh</td>
                <td valign=\"top\">:</td>
                <td valign=\"top\"><i>".(isset($arr['last_update'])?$arr['last_update']:"")."</i></td>
              </tr>";
            } */
            $ret .= "
              <tr>
                <td valign=\"top\">&nbsp;</td>
                <td valign=\"top\">&nbsp;</td>
                <td valign=\"top\">
                  <input class=\"tombol\" type=\"button\" name=\"btn_kembali\" id=\"btn_kembali\" value=\"<< Kembali\" />
                  <input class=\"tombol\" type=\"submit\" name=\"button\" id=\"button\" value=\"   Simpan   \" />
                </td>
              </tr>
            </table>
            </fieldset>
            </form>
              ";
            
            return $ret;
        }
        
        function simpan($arr, $arr_2, $is_update=false, $id='') {
            if ($is_update==FALSE) {
                $this->db->insert('arsip', $arr);
                $arr_2['pta_id_arsip'] = $this->db->insert_id();
                $this->db->insert('arsip_surat', $arr_2);
            } else {
                $this->db->where('idpta_arsip', $id);
                $this->db->update('arsip', $arr);

                $this->db->where('pta_id_arsip', $id);
                $this->db->update('arsip_surat', $arr_2);
            }
        }
        
        
        function simpan_disposisi($arr, $arr_1, $arr_2, $is_update=false, $id='') {
            if ($is_update==FALSE) {
                $this->db->insert('arsip', $arr);
                $this->db->insert('arsip_terusan', $arr_1);
                
                if ($arr_2->isi_disposisi != "") {
                $arr_2['id_terusan'] = $this->db->insert_id();
                $this->db->insert('arsip_disposisi', $arr_2);
                }
                
            } else {
                //$this->db->where('idpta_arsip', $id);
                //$this->db->update('arsip', $arr);

               // $this->db->where('pta_id_arsip', $id);
               // $this->db->update('arsip_surat', $arr_2);
            }
        }
        
	}
?>